

var selectifierHighlightClassName = 'selectifierHighlight';

function addEventHandler(element, eventType, handler) {
	 if (element.addEventListener) 
		 element.addEventListener(eventType, handler, false);
	 else if (element.attachEvent)
		 element.attachEvent('on' + eventType, handler); 
}

function highlightSelectedText(selectedText) {
    $(document.body).highlight(selectedText, { 
        className: selectifierHighlightClassName
    });
}

function unHighlightSelectedText() {
    $(document.body).unhighlight({ 
        className: selectifierHighlightClassName
    });
}

function checkSelectedText(evt) {
	var selection;
        
	if (window.getSelection) 
	  selection = window.getSelection();
	else if (document.selection) 
	  selection = document.selection.createRange();

	var selectedText = selection.toString().trim();
    
    if (selectedText !== '') {
        unHighlightSelectedText();
        highlightSelectedText(selectedText);
    }
    else {
        unHighlightSelectedText();
    }
}

addEventHandler(document.body, 'mouseup', checkSelectedText);
